import { statusType, statusEnum } from '../type';

class Modal {
  status: statusType;
  constructor(status: statusType) {
    this.status = status;
  }
  get className() {
    let classStr = 'modal';
    return classStr + ' ' + this.status;
  }
  static checkStatusIsExist(status: statusType, statusEnum: statusEnum ) {
    for(var key in statusEnum as any) {
      if(key === status){
        return true;
      }
    }
    return false;
  }
  static log (info: any) {
    console.log(info);
  }
}

class SuccessModal extends Modal {
  constructor() {
    super(statusEnum[0] as statusType);
  }
  goTo (url: string) {
    window.open(url);
  }
}
class WarnModal extends Modal {
  constructor() {
    super(statusEnum[1] as statusType);
  }
  log(info:any) {
    Modal.log('【WarnInfo】' + info)
  }
}
class ErrorModal extends Modal {
  constructor() {
    super(statusEnum[2] as statusType);
  }
  log(info:any) {
    Modal.log('【ErrorInfo】' + info)
  }
}

class ModalFactory {
  dom: any;
  constructor(dom: any) {
    this.dom = dom;
  }
  createModal(status: statusType) {
    const statusIsExist = Modal.checkStatusIsExist(status, statusEnum as any);
    if(!statusIsExist) {
      throw new Error('Modal type is incorrect.');
    }
    const dom = this.dom;
    let modal = null;
    switch (statusEnum[status]) {
      case statusEnum.success:
        modal = new SuccessModal();
        break;
      case statusEnum.warn:
        modal = new WarnModal();
        break;
      case statusEnum.error:
        modal = new ErrorModal();
        break;
      default:
        break;
    }
    const m = dom.querySelector('.modal');
    const modalDom = m || document.createElement('div');
    modalDom.className = modal?.className;
    dom.appendChild(modalDom);
    return {
      // @ts-ignore
      log: modal.log,
      // @ts-ignore
      goTo: modal.goTo,
    }
  }
}
export default ModalFactory;
